package com.hundun.vision.biz.utils;

import com.hundun.vision.biz.dto.KlineBarDTO;
import org.ta4j.core.Bar;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseBar;
import org.ta4j.core.BaseBarSeriesBuilder;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.num.DoubleNum;

import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

/**
 * @author ：RuoChen
 * @date ：09:02 2024/2/27
 * @description：kline工具类
 */
public class KlineUtils {

    /**
     * 构建时间序列
     *
     * @param klineBars
     * @param interval
     * @return
     */
    public static BarSeries buildBarSeries(List<KlineBarDTO> klineBars, String interval) {
        BarSeries series = new BaseBarSeriesBuilder().withNumTypeOf(DecimalNum.class).build();
        Duration barDuration = Duration.ofSeconds(TimeUtils.getDuration(interval));
        for (KlineBarDTO klineBar : klineBars) {
            ZonedDateTime endTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(klineBar.getEndTimestamp()), ZoneId.systemDefault());
            Bar newBar = new BaseBar(barDuration, endTime, klineBar.getOpenPrice(), klineBar.getHighPrice(),
                    klineBar.getLowPrice(), klineBar.getClosePrice(), klineBar.getVolume(), klineBar.getAmount());
            series.addBar(newBar);
        }
        return series;
    }
}
